<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//加密函数
function lock_url($txt, $key = 'zhuoyuexiazai')
{
    $txt = $txt . $key;
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
    $nh = rand(0, 64);
    $ch = $chars[$nh];
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt = base64_encode($txt);
    $tmp = '';
    $i = 0;
    $j = 0;
    $k = 0;
    for ($i = 0; $i < strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
        $tmp .= $chars[$j];
    }
    return urlencode(base64_encode($ch . $tmp));
}

//解密函数
function unlock_url($txt, $key = 'zhuoyuexiazai')
{
    $txt = base64_decode(urldecode($txt));
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
    $ch = $txt[0];
    $nh = strpos($chars, $ch);
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt = substr($txt, 1);
    $tmp = '';
    $i = 0;
    $j = 0;
    $k = 0;
    for ($i = 0; $i < strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
        while ($j < 0) $j += 64;
        $tmp .= $chars[$j];
    }
    return trim(base64_decode($tmp), $key);
}

function p($a)
{
    echo '<pre>';
    print_r($a);
    die;

}

//二维数组
function two_dimensional_array($list, $key)
{
    $ref = [];
    foreach ($list as $rkey => $val) {
        if ($val[$key] == 0) {
            $ref[] = $val;
        }
    }

    foreach ($ref as $rkey1 => $val1) {
        foreach ($list as $rkey2 => $val2) {
            if ($val1['id'] == $val2[$key]) {
                $ref[$rkey1]['children'][] = $val2;
            }
        }
    }
    return $ref;
}

function juli_str($lat, $lng)
{
    $juli = " ROUND(
			        6378.138 * 2 * ASIN(
			            SQRT(
			                POW(
			                    SIN(
			                        (
			                            $lat * PI() / 180 - lat * PI() / 180
			                        ) / 2
			                    ),
			                    2
			                ) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * POW(
			                    SIN(
			                        (
			                            $lng * PI() / 180 - lng * PI() / 180
			                        ) / 2
			                    ),
			                    2
			                )
			            )
			        ) * 1000
			    ) AS juli
			";
    return $juli;
}

/**
 * 获取网路图片的长宽
 * @param unknown $url
 * @return string[]|unknown[]
 */
function getImageinfo($url)
{
    $result = array(
        'width' => '',
        'height' => '',
        'size' => '',
    );
    $imageInfo = getimagesize($url);
    $result['width'] = $imageInfo[0];
    $result['height'] = $imageInfo[1];

    $headerInfo = get_headers($url, true);
    $result['size'] = $headerInfo['Content-Length'];

    return $result;
}

/**
 * 学历
 * @param unknown $id
 * @return string
 */
function education($id)
{
    $arr = [
        '1' => '专科及以下',
        '2' => '本科',
        '3' => '研究生',
        '4' => '博士',
    ];
    if (key_exists($id, $arr)) {
        return $arr[$id];
    } else {
        return '未知';
    }
}

/**
 * 轮次
 * @param unknown $id
 * @return string
 */
function turns($id)
{

    $arr = [
        '0' => '种子轮',
        '1' => '天使轮',
        '2' => 'A轮',
        '3' => 'B轮',
        '4' => 'C轮',
        '5' => '战略投资',
        '6' => '已被收购',
        '7' => '新三板',
        '8' => '尚未获投',
        '9' => '不明确',
        '10' => 'A+轮',
        '11' => 'B+轮'
    ];
    if (key_exists($id, $arr)) {
        return $arr[$id];
    } else {
        return '未知';
    }
}

/**
 * 企业服务
 * @param unknown $id
 * @return string
 */
function service_type($id)
{

    $arr = [
        '1' => '企业服务1',
        '2' => '企业服务2',
        '3' => '企业服务3',
        '4' => '企业服务4',
    ];
    if (key_exists($id, $arr)) {
        return $arr[$id];
    } else {
        return '未知';
    }
}

/**
 * curl-post
 **/
function post_curls($url, $data)
{
    $ch = curl_init();
    // print_r($ch);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $return = curl_exec($ch);
    curl_close($ch);
    return $return; // 返回数据，json格式

}

function dd($data)
{
    echo '<pre>';
    var_dump($data);
    die();
}

function company_type($id)
{
    $arr = [
        '1' => '有限责任公司',
        '2' => '股份有限公司',
        '3' => '集体所有制',
        '4' => '国有企业',
        '5' => '个体工商户',
        '6' => '有限合伙',
        '7' => '普通合伙',
        '8' => '外商投资企业',
        '9' => '港、澳、台',
        '10' => '联营企业',
        '11' => '私营企业',
    ];
    if ($id == 0) {
        return $arr;
    }
    if (key_exists($id, $arr)) {
        return $arr[$id];
    } else {
        return '未知';
    }
}

function ipo($id)
{
    $arr = [
        '1' => 'is_ipo',
        '2' => 'is_inspect',
        '3' => 'is_report',
        '4' => 'is_market'
    ];
    if (key_exists($id, $arr)) {
        return $arr[$id];
    } else {
        return '未知';
    }
}

function httpRequest($url, $type = "POST", $keysArr = array(), $headersArr = array(), $timeOut = 30)
{
    if ($type == "POST") {
        $ch = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }

        $aPOST = array();
        foreach ($keysArr as $key => $val) {
            $aPOST[] = $key . "=" . $val;
        }
        $strPOST = trim(implode("&", $aPOST));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        //带上headers
        if ($headersArr) {
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headersArr);
        }


        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);

        $sContent = curl_exec($ch);
        $aStatus = curl_getinfo($ch);
//            var_dump($aStatus);exit;
        curl_close($ch);
        if (intval($aStatus["http_code"]) == 200) {
            if ($sContent) {
                //echo 111;
                return json_decode($sContent, true);
            }
//                echo 2222;
            return $sContent;
        }
    } else {
        $url = combineURL($url, $keysArr);
        $ch = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_SSLVERSION, 0); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($headersArr) {
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headersArr);
        }

        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);


//            // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
//            curl_setopt($ch, CURLOPT_HEADER, true);
//            // 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文
//            curl_setopt($ch, CURLOPT_NOBODY, true);
//            //至关重要，CURLINFO_HEADER_OUT选项可以拿到请求头信息
//            curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
        $sContent = curl_exec($ch);

//            if ($error == curl_error($ch)) {
//                var_export($error);
//                return false;
//            }

        $aStatus = curl_getinfo($ch);

//            echo "<pre>";
//            var_export($sContent);
//            var_export($aStatus);
//            exit;

        curl_close($ch);
        if (intval($aStatus["http_code"]) == 200) {
            if ($sContent) {
                return json_decode($sContent, true);
            }
            return $sContent;
        }
    }
}

function combineURL($baseURL, $keysArr)
{
    if (empty($keysArr) || !is_array($keysArr)) return $baseURL;
    $combined = $baseURL . "?";
    $valueArr = array();
    foreach ($keysArr as $key => $val) {
        $valueArr[] = "$key=" . $val;
    }
    $keyStr = trim(implode("&", $valueArr));
    $combined .= ($keyStr);
    return $combined;
}

function error($status = -1, $msg = 'error')
{
    return json_encode(['status' => $status, 'msg' => $msg, 'data' => []]);
}

function sendtest($url, $data = array()){
               $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if(!$data){
return 'data is null';
}
if(is_array($data))
{
 $data = json_encode($data);
}
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data), 'Cache-Control: no-cache', 'Pragma: no-cache'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
$errorno = curl_errno($curl);
if($errorno){
return $errorno;
}
curl_close($curl);
return $res;

}

